Skip to contentMethod: chooseRandomMove(TicTacToePlayer, TicTacToeState)
      1: /*
2:  * Copyright © 2021 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel21-tictactoe-core.
5:  *
6:  * ipspiel21-tictactoe-core is free software: you can redistribute it and/or modify it under
7:  * the terms of the GNU General Public License as published by the Free Software
8:  * Foundation, either version 3 of the License, or (at your option) any later
9:  * version.
10:  *
11:  * ipspiel21-tictactoe-core is distributed in the hope that it will be useful, but WITHOUT
12:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13:  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14:  * details.
15:  *
16:  * You should have received a copy of the GNU General Public License along with
17:  * ipspiel21-tictactoe-core. If not, see <http://www.gnu.org/licenses/>.
18:  */
19: package de.fhdw.gaming.ipspiel21.tictactoe.core.domain.impl;
20: 
21: import java.util.ArrayList;
22: import java.util.List;
23: import java.util.Map;
24: import java.util.Optional;
25: import java.util.Random;
26: 
27: import de.fhdw.gaming.core.domain.DefaultGame;
28: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
29: import de.fhdw.gaming.ipspiel21.tictactoe.core.domain.TicTacToeField;
30: import de.fhdw.gaming.ipspiel21.tictactoe.core.domain.TicTacToeFieldState;
31: import de.fhdw.gaming.ipspiel21.tictactoe.core.domain.TicTacToeGame;
32: import de.fhdw.gaming.ipspiel21.tictactoe.core.domain.TicTacToeMoveChecker;
33: import de.fhdw.gaming.ipspiel21.tictactoe.core.domain.TicTacToePlayer;
34: import de.fhdw.gaming.ipspiel21.tictactoe.core.domain.TicTacToeState;
35: import de.fhdw.gaming.ipspiel21.tictactoe.core.domain.TicTacToeStrategy;
36: import de.fhdw.gaming.ipspiel21.tictactoe.core.moves.TicTacToeMove;
37: import de.fhdw.gaming.ipspiel21.tictactoe.core.moves.factory.TicTacToeMoveFactory;
38: import de.fhdw.gaming.ipspiel21.tictactoe.core.moves.impl.TicTacToeDefaultMoveFactory;
39: 
40: /**
41:  * Implements the Tic Tac Toe game.
42:  */
43: final class TicTacToeGameImpl extends DefaultGame<TicTacToePlayer, TicTacToeState, TicTacToeMove, TicTacToeStrategy>
44:         implements TicTacToeGame {
45: 
46:     /**
47:      * The number of rows (and columns) of the board.
48:      */
49:     private final int boardSize;
50:     /**
51:      * The move factory.
52:      */
53:     private final TicTacToeMoveFactory moveFactory;
54: 
55:     /**
56:      * Creates a TicTacToe game.
57:      *
58:      * @param id                        The ID of this game.
59:      * @param initialState              The initial state of the game.
60:      * @param strategies                The players' strategies.
61:      * @param maxComputationTimePerMove The maximum computation time per move in seconds.
62:      * @param moveChecker               The move checker.
63:      * @param observerFactoryProvider   The {@link ObserverFactoryProvider}.
64:      * @throws IllegalArgumentException if the player sets do not match.
65:      * @throws InterruptedException     if creating the game has been interrupted.
66:      */
67:     TicTacToeGameImpl(final int id, final TicTacToeState initialState, final Map<String, TicTacToeStrategy> strategies,
68:             final long maxComputationTimePerMove, final TicTacToeMoveChecker moveChecker,
69:             final ObserverFactoryProvider observerFactoryProvider)
70:             throws IllegalArgumentException, InterruptedException {
71: 
72:         super(id, initialState, strategies, maxComputationTimePerMove, moveChecker, observerFactoryProvider);
73:         this.boardSize = initialState.getBoard().getSize();
74:         this.moveFactory = new TicTacToeDefaultMoveFactory();
75:     }
76: 
77:     @Override
78:     public Optional<TicTacToeMove> chooseRandomMove(final TicTacToePlayer player, final TicTacToeState state) {
79:         final List<TicTacToeField> fields = new ArrayList<>(
80:                 state.getBoard().getFieldsBeing(TicTacToeFieldState.EMPTY).values());
81: 
82:•        if (fields.isEmpty()) {
83:             return Optional.empty();
84:         } else {
85:             final int index = new Random().nextInt(fields.size());
86:             final TicTacToeField field = fields.get(index);
87:             return Optional.of(this.moveFactory.createPlaceMarkMove(player.isUsingCrosses(), field.getPosition()));
88:         }
89:     }
90: 
91:     @Override
92:     public String toString() {
93:         return String
94:                 .format("TicTacToeGame[id=%d, boardSize=%d, %s]", this.getId(), this.boardSize, this.gameToString());
95:     }
96: 
97:     @Override
98:     public int getBoardSize() {
99:         return this.boardSize;
100:     }
101: }